﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 套餐数据库操作类
    /// </summary>
    public class DaoPackage : DAO
    {
        /// <summary>
        /// 添加套餐
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Add(MPackage m)
        {
            String sql = "insert into package([user_id],package_type,end_date)values(@user_id,@package_type,@end_date)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@package_type", DbType.Int32, m.package_type);
            db.AddInParameter(cmd, "@end_date", DbType.DateTime, m.end_date);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MPackage GetEntity(int id)
        {
            String sql = "select * from package where id = " + id;
            object obj = BindEntity(sql, new MAdvert());
            return obj == null ? null : (MPackage)obj;
        }

        /// <summary>
        /// 删除一条广告
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Delete(int id, int userId)
        {
            String sql = "delete package where id = " + id + " and user_id = " + userId;
            return ExecuteNonQuery(sql) > 0;
        }

        /// <summary>
        /// 隐藏该条广告
        /// </summary>
        /// <param name="id">广告id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Hide(int id, int userId)
        {
            String sql = "update package set [state] = 0 where id = " + id + " and user_id = " + userId;
            return ExecuteNonQuery(sql) > 0;
        }


        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetList(int pageIndex, int pageSize, int userId)
        {
            String fieldList = "id,[date],end_date,remark";
            String orderBy = "id";
            String where = "state = 1 and user_id = " + userId;
            String tableName = "v_package";
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }
    }
}
